<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<!-- 网页使用的语言 -->
<html lang="zh-CN">
<head>
    <!-- 指定字符集 -->
    <meta charset="utf-8">
    <!-- 使用Edge最新的浏览器的渲染方式 -->
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!-- viewport视口：网页可以根据设置的宽度自动进行适配，在浏览器的内部虚拟一个容器，容器的宽度与设备的宽度相同。
    width: 默认宽度与设备的宽度相同
    initial-scale: 初始的缩放比，为1:1 -->
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>Bootstrap模板</title>

    <!-- 1. 导入CSS的全局样式 -->
    <link href="../css/bootstrap.min.css" rel="stylesheet">
    <!-- 2. jQuery导入，建议使用1.9以上的版本 -->
    <script src="../js/jquery-2.1.0.min.js"></script>
    <!-- 3. 导入bootstrap的js文件 -->
    <script src="../js/bootstrap.min.js"></script>
    <style type="text/css">
        td, th {
            text-align: center;
        }
    </style>
</head>
<body>
<div class="container">
    <div style="text-align: right;color: red;">
        <c:if test="${not empty user}">
            欢迎 ${user.username}
        </c:if>
    </div>
    <h3 style="text-align: center">分页查询联系人</h3>
    <form action="/contact" method="post" onsubmit="return checkDelete()">
        <input type="hidden" name="method" value="deleteMulti"/>
        <table border="1" class="table table-bordered table-hover">
            <tr class="success">
                <th>
                    <%-- TODO:复选框全选和全不选操作 --%>
                    <input type="checkbox" onclick="checkAll(this.checked)">
                </th>
                <th>编号</th>
                <th>姓名</th>
                <th>性别</th>
                <th>年龄</th>
                <th>籍贯</th>
                <th>QQ</th>
                <th>邮箱</th>
                <th>操作</th>
            </tr>
            <c:forEach items="${PageInfo.list}" var="contact">
                <tr>
                    <td>
                        <input type="checkbox" class="ck_item" name="ids" value="${contact.id}">
                    </td>
                    <td>${contact.id}</td>
                    <td>${contact.name}</td>
                    <td>${contact.sex}</td>
                    <td>${contact.age}</td>
                    <td>${contact.address}</td>
                    <td>${contact.qq}</td>
                    <td>${contact.email}</td>
                    <td><a class="btn btn-default btn-sm"
                           href="/contact?method=findContactById&id=${contact.id}&name=${contact.name}">修改</a>&nbsp;
                        <a class="btn btn-default btn-sm" href="JavaScript:deleteById(${contact.id})">删除</a></td>
                </tr>
            </c:forEach>
            <tr>
                <td colspan="9" align="center">
                    <input type="submit" class="btn btn-danger" value="删除选中">
                    <a class="btn btn-primary" href="/contact/add.jsp">添加联系人</a>
                    <input class="btn btn-default" type="button" value="返回" onclick="jump()"/></td>
            </tr>
        </table>
    </form>
</div>

<form action="/contact" class="text-center">
    <input type="hidden" name="method" value="findByPage">
    每页显示<select name="pageSize" id="pageSize">
    <c:forEach begin="1" end="${PageInfo.totalCount}" step="1" var="j">
        <c:if test="${PageInfo.pageSize!=j}">
            <option value="${j}">${j}</option>
        </c:if>
        <c:if test="${PageInfo.pageSize==j}">
            <option selected value="${j}">${j}</option>
        </c:if>
    </c:forEach>
</select>条
    <input type="submit" value="确定">
</form>
<nav aria-label="Page navigation" class="text-center">
    <ul class="pagination">
        <c:if test="${PageInfo.currentPage==1}">
            <li class="disabled">
                <span aria-hidden="true">&laquo;</span>
            </li>
        </c:if>
        <c:if test="${PageInfo.currentPage>1}">
            <li>
                <a href="/contact?method=findByPage&currentPage=${PageInfo.currentPage-1}&pageSize=${PageInfo.pageSize}" aria-label="Previous">
                    <span aria-hidden="true">&laquo;</span>
                </a>
            </li>
        </c:if>
        <c:forEach begin="${startIndex}" end="${endIndex}" step="1" var="i">
            <c:if test="${PageInfo.currentPage==i}">
                <li class="active"><a href="/contact?method=findByPage&currentPage=${i}&pageSize=${PageInfo.pageSize}">${i}</a></li>
            </c:if>
            <c:if test="${PageInfo.currentPage!=i}">
                <li><a href="/contact?method=findByPage&currentPage=${i}&pageSize=${PageInfo.pageSize}">${i}</a></li>
            </c:if>
        </c:forEach>
        <c:if test="${PageInfo.currentPage==PageInfo.totalPage}">
            <li class="disabled">
                <span aria-hidden="true">&raquo;</span>
            </li>
        </c:if>
        <c:if test="${PageInfo.currentPage<PageInfo.totalPage}">
            <li>
                <a href="/contact?method=findByPage&currentPage=${PageInfo.currentPage+1}&pageSize=${PageInfo.pageSize}" aria-label="Previous">
                    <span aria-hidden="true">&raquo;</span>
                </a>
            </li>
        </c:if>
    </ul>
</nav>
</body>
</html>
<script>
    function deleteById(id) {
        //alert(id);
        let flag = confirm("您确定删除吗?")
        if (flag) {
            //发送根据id删除联系人的请求
            location.href = "/contact?method=deleteContactById&id=" + id;
        }
    }

    function jump() {
        location.href = "/index.jsp";
    }

    function checkAll(flag) {
        let itemArr = document.getElementsByClassName("ck_item");
        for (let i = 0; i < itemArr.length; i++) {
            itemArr[i].checked = flag;
        }
    }

    function checkDelete() {
        return confirm("确定删除吗?")
    }
</script>